package com.zxpt.quetion.controller;


import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.zxpt.quetion.common.Result;
import com.zxpt.quetion.pojovo.QuestionItemVo;
import com.zxpt.quetion.pojovo.QuestionMouldVo;
import com.zxpt.quetion.service.IQuestionMouldService;
import com.zxpt.quetion.service.QuestionCountService;
import com.zxpt.quetion.utils.ExcelUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/questionCount")
@Slf4j
public class QuestionCount {
    @Resource
    QuestionCountService questionCountService;
    @Resource
    IQuestionMouldService mouldService;

    /**
     * 获得
     */
    @SneakyThrows
    @GetMapping("/getByMId")
    public void  getByMId (HttpServletResponse response,@RequestParam("mid") String mid,@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        QuestionMouldVo mould=mouldService.getById(mid);
        String fileName = URLEncoder.encode(mould.getTitle(), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        List<List<Object>> baseRowModel=questionCountService.getByMId(mid,startTime,endTime);
        OutputStream outputStream = null;
        ExcelWriter writer = null;
        Sheet sheet=ExcelUtil.initSheet;
        List<String> headTitle_A = new ArrayList<String>();
        List<String> headTitle_B = new ArrayList<String>();
        List<String> headTitle_C = new ArrayList<String>();
        List<List<String>> head = new ArrayList<>();
        Integer pageviews = mould.getPageviews();
        Integer recovery=mould.getRecovery();
        BigDecimal pageviews_b = BigDecimal.valueOf(pageviews);
        BigDecimal recovery_b = BigDecimal.valueOf(recovery);
        BigDecimal rateRecovery = BigDecimal.ZERO;
        if(recovery != 0){
            rateRecovery = recovery_b.divide(pageviews_b,2, BigDecimal.ROUND_HALF_EVEN);
        }
        //主标题-前言-其他
        headTitle_A.add("《"+mould.getTitle()+"》");
        headTitle_A.add("“"+mould.getPrefix()+"”");
        headTitle_A.add("浏览量");
        headTitle_A.add(pageviews.toString());
        head.add(headTitle_A);
        //
        headTitle_B.add("");
        headTitle_B.add("");
        headTitle_B.add("回收量");
        headTitle_B.add(recovery.toString());
        head.add(headTitle_B);
        //
        headTitle_C.add("");
        headTitle_C.add("");
        headTitle_C.add("回收率");
        headTitle_C.add(rateRecovery.multiply(new BigDecimal("100"))+"%");
        head.add(headTitle_C);

        sheet.setHead(head);
        try{
             outputStream = response.getOutputStream();
             writer=EasyExcelFactory.getWriter(outputStream);
             writer.write1(baseRowModel, sheet);
        }catch (Exception e){
            log.error("excel文件导出失败, 失败原因：{}", e);
        }finally {
            try {
                if(writer != null){
                    writer.finish();
                }
                if(outputStream != null){
                    outputStream.close();
                }

            } catch (IOException e) {
                log.error("excel文件导出失败, 失败原因：{}", e);
            }
        }

    }


}
